#!/usr/bin/env bash

# This script is meant to be run as `./releng/prepare-y-bump` by a
# human Emissary maintainer who is designating that we are starting
# work on a new X.Y version.

set -euE -o pipefail

this_xy=$(git describe --tags --match='v*'|cut -d. -f1,2|sed 's/^v//')
next_xy=${this_xy%.*}.$((${this_xy##*.}+1))

chart_this_xy=$(git describe --tags --match='chart/v*'|cut -d. -f1,2|sed 's,^chart/v,,')
chart_next_xy=${chart_this_xy%.*}.$((${chart_this_xy##*.}+1))

refs=()

if test -z "$(git for-each-ref "refs/remotes/*/release/v${this_xy}")"; then
	# We are on 'master', which will become the next_xy branch,
	# and we need to create a 'release/v${this_xy}' branch.

	git branch "release/v${this_xy}"
	refs+=("release/v${this_xy}")

	git branch "${USER}/v${next_xy}"
	git switch "${USER}/v${next_xy}"
	refs+=("${USER}/v${next_xy}")

	echo 'Editing README.md...'
	gawk -i inplace \
	     -v this_xy="${this_xy}" \
	     -v next_xy="${next_xy}" \
	     '
	       /\[`master`\].*branch for/ {
	         print "- [`master`](https://github.com/emissary-ingress/emissary/tree/master) - branch for Emissary-ingress " next_xy ".z work (:heavy_check_mark: upcoming release)"
	         print "- [`release/v" this_xy "`](https://github.com/emissary-ingress/emissary/tree/release/v" this_xy ") - branch for Emissary-ingress " this_xy ".z work (:heavy_check_mark: current latest)"
	       }
	       ! /\[`master`\].*branch for/ { print }
	     ' \
	     README.md
else
	# We are on 'release/v{this_xy}' (presumably 'x' is not the
	# latest major version), and we need to create a
	# 'release/${next_xy}' branch.

	git branch "release/v${next_xy}"
	git switch "release/v${next_xy}"
	refs+=("release/v${next_xy}")

	echo 'Editing README.md...'
	gawk -i inplace \
	     -v next_xy="${next_xy}" \
	     '
	       /\[`release\/v'"${this_xy/./'\.'}"'`\].*branch for/ {
	         print "- [`release/v" next_xy "`](https://github.com/emissary-ingress/emissary/tree/release/v" next_xy ") - branch for Emissary-ingress " next_xy ".z work (:heavy_check_mark: upcoming release)"
	       }
	       { print }
	     ' \
	     README.md
fi

echo 'Editing docs/releaseNotes.yml...'
if ! grep -q -F -e "- version: ${next_xy}.0" docs/releaseNotes.yml; then
	gawk -i inplace \
	     -v next_xy="${next_xy}" \
	     '
	       BEGIN { done=0 }
	       /^  - version:/ && !done {
	         print "  - version: " next_xy ".0"
	         print "    date: '\''TBD'\''"
	         print "    notes: []"
	         print ""
	         done=1
	       } \
	       { print }
	     ' \
	    docs/releaseNotes.yml
fi

echo 'Editing charts/emissary-ingress/CHANGELOG.md...'
if ! grep -q -e "^## v{next_xy}\.0(\$| )" charts/emissary-ingress/CHANGELOG.md; then
	gawk -i inplace \
	     -v next_xy="${next_xy}" \
	     -v chart_next_xy="${chart_next_xy}" \
	     '
	       BEGIN { done=0 }
	       { del=0 }
	       /^## Next Release/ && !done {
	         print "## v" chart_next_xy ".0 (not yet released)"
	         print ""
	         print "- Upgrade Emissary to v" next_xy ".0 [CHANGELOG](https://github.com/emissary-ingress/emissary/blob/master/CHANGELOG.md)"
	         print ""
	         done=1
	         del=1
	       }
	       /^## v/ && !done {
	         print "## v" chart_next_xy ".0 (not yet released)"
	         print ""
	         print "- Upgrade Emissary to v" next_xy ".0 [CHANGELOG](https://github.com/emissary-ingress/emissary/blob/master/CHANGELOG.md)"
	         print ""
	         done=1
	       }
	       /no changes yet/ { del=1 }
	       !del { print }
	     ' \
	     charts/emissary-ingress/CHANGELOG.md
fi

echo "Running 'make generate-fast'..."
make generate-fast

echo
echo 'Commiting changes...'
git add .
git commit --signoff --message="Prepare to work on v${next_xy}.0"

echo 'Creating tags...'
git tag --annotate --message="Start work on Emissary v${next_xy}.0" "v${next_xy}.0-dev"
refs+=("v${next_xy}.0-dev")
git tag --annotate --message="Start work on Emissary chart v${chart_next_xy}.0 for Emissary v${next_xy}.0" "chart/v${chart_next_xy}.0-dev"
refs+=("chart/v${chart_next_xy}.0-dev")

echo
echo "======================================================================"
echo
echo "All done!"
echo "Look over the commit that has been made locally;"
echo "when you are ready, run the following command to push it:"
echo
echo "    git push origin ${refs[*]}"
echo
